package cn.com.template.controller.project;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.com.commons.Paginator;
import cn.com.dal.dataobject.cs.ProjectDO;
import cn.com.service.cs.ProjectService;
import cn.com.support.context.UserContext;
import cn.com.support.message.MessageResult;
import cn.com.support.utils.TemplateUtils;
import cn.com.template.ui.Flexigrid;
import cn.com.template.utils.FlexigridUtil;

/**
 * 
 * 公司项目管理控制器
 * 
 * @since 1.0 2012-2-21 下午11:03:53
 * @version 1.0
 * @author Zoe
 */
@Controller
@RequestMapping("/project/project")
public class ProjectController {

	@Autowired
	private ProjectService projectService;

	/**
	 * 跳转页面
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping
	public ModelAndView forward(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("/project/project");
		return mav;
	}

	/**
	 * 删除数据
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public MessageResult<Long> delete(HttpServletRequest request) {
		return projectService.deleteById(TemplateUtils.getParams(Long.class,
				request, "id"));
	}

	/**
	 * 查询所有
	 * 
	 * @param condition
	 *            查询条件
	 * @return
	 */
	@RequestMapping("/selectList")
	@ResponseBody
	public List<?> selectList(HttpServletRequest request,
			HttpServletResponse response) {
		return projectService.selectList(TemplateUtils.bulidBeanMap(request,
				ProjectDO.class));
	}

	/**
	 * 根据ID查询
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/selectById")
	@ResponseBody
	public MessageResult<ProjectDO> selectById(@RequestParam("id") Long id) {
		return projectService.selectById(id);
	}

	/**
	 * 分页查询
	 * 
	 * @param paginator
	 * @return
	 */
	@RequestMapping("/selectForPage.json")
	@ResponseBody
	public Flexigrid selectForPage(HttpServletRequest request,
			HttpServletResponse response) {
		// 封装参数
		Paginator<Object, List<ProjectDO>> paginator = new Paginator<Object, List<ProjectDO>>();
		Integer page = TemplateUtils
				.getParam(Integer.class, request, "page", 1);
		Map<Object, Object> condition = TemplateUtils.bulidBeanMap(request,
				ProjectDO.class);
		// 封装查询时间
		TemplateUtils.wappedStartAndEndTime(condition, request);
		paginator.setCurrentPage(page);
		paginator.setCondition(condition);
		// 分页查询
		projectService.selectByPage(paginator);
		// 返回结果
		return FlexigridUtil.formatFlexigridData(paginator);
	}

	/**
	 * 更新项目
	 * 
	 * @param projectDO
	 * @return
	 */
	@RequestMapping("/updateById")
	@ResponseBody
	MessageResult<Integer> updateById(HttpServletRequest request) {
		// 封装参数
		ProjectDO projectDO = TemplateUtils.bulidBean(request, ProjectDO.class);
		projectDO.setModifier(UserContext.getUsername());
		// 返回结果
		return projectService.updateById(projectDO);
	}
	
	/**
	 * 更新项目
	 * 
	 * @param projectDO
	 * @return
	 */
	@RequestMapping("/insert")
	@ResponseBody
	MessageResult<Long> insert(HttpServletRequest request) {
		// 封装参数
		ProjectDO projectDO = TemplateUtils.bulidBean(request, ProjectDO.class);
		projectDO.setCreator(UserContext.getUsername());
		// 返回结果
		return projectService.insert(projectDO);
	}
	
	//ADD BY ZOE for JQUERY AUTOCOMPELETE
	@RequestMapping("/autoFillProjct")
	@ResponseBody
	public String autoFillProjct(@RequestParam String q, HttpServletResponse response){
		String responseStr = null;
		response.setCharacterEncoding("UTF-8");
		q = q.toUpperCase();
		Map<Object, Object> paramMap = new HashMap<Object, Object>();
		try {
			q = URLDecoder.decode(q,"UTF-8");
			paramMap.put("name", q);
			List<ProjectDO> resultLst = projectService.selectList(paramMap);
//			List<Map<String, String>> res = new ArrayList<Map<String,String>>();
//			Map<String, String> map = null;
//			for(ProjectDO projectDO : resultLst){
//				map = new HashMap<String, String>();
//				map.put("id", projectDO.getId()+"");
//				map.put("name", projectDO.getName());
//				res.add(map);
//			}
//			StringWriter writer = new StringWriter();
//			ObjectMapper mapper = new ObjectMapper();
//			mapper.writeValue(writer, res);
//			responseStr = writer.toString();
//			System.out.println(responseStr);
			PrintWriter writer = response.getWriter();
			List<Map<String, String>> res = new ArrayList<Map<String,String>>();
			Map<String, String> map = null;
			for(ProjectDO projectDO : resultLst){
				map = new HashMap<String, String>();
				map.put("id", projectDO.getId()+"");
				map.put("name", projectDO.getName());
				res.add(map);
			}
			ObjectMapper mapper = new ObjectMapper();
			mapper.writeValue(writer, res);
			responseStr = writer.toString();
			writer.write(responseStr);
			writer.flush();
			writer.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
